package com.hzqy.service.epg;

import java.io.IOException;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.hzqy.persistence.dao.epg.WchatTvOrgDao;
import com.hzqy.web.vo.SysAreaStreetVo;
import com.hzqy.web.vo.WchatTvUserVo;
import com.hzqy.web.vo.epg.AcInterfaceVo;
import com.hzqy.web.vo.epg.AcTelephoneVo;
import com.hzqy.web.vo.epg.WchatTvOrgVo;

@Service
public class WchatTvOrgService {

	@Resource
	private WchatTvOrgDao wchatTvOrgDao;

	public Map<String, Object> selectByUserId(WchatTvUserVo wtOrgVo) {
		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("f_wtut_serverid", wtOrgVo.getF_wtut_serverid());
		WchatTvUserVo  wchatTvOrgVo= wchatTvOrgDao.selectINFO(map2);
		Map<String, Object> map = new HashMap<String, Object>();
		if (wchatTvOrgVo != null ) {
			map.put("resultFlag", true);
			map.put("result", wchatTvOrgVo);
		}else {
			map.put("resultFlag", false);
		}
		return map;
	}


	public int updateStatus(WchatTvUserVo wtOrgVo) {
		return wchatTvOrgDao.updateStatus(wtOrgVo);
	}

	public Map<String, Object> selectByUserPhone(WchatTvUserVo wtOrgVo) {
		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("f_wtut_phone", wtOrgVo.getF_wtut_phone());
		WchatTvUserVo  wchatTvOrgVo= wchatTvOrgDao.selectINFO(map2);
		Map<String, Object> map = new HashMap<String, Object>();
		if (wchatTvOrgVo != null ) {
			if (wchatTvOrgVo.getF_wtut_serverid() == null || "".equals(wchatTvOrgVo.getF_wtut_serverid().trim())) {
				//执行修改
				int updateResult = wchatTvOrgDao.updateService(wtOrgVo);
				if (updateResult > 0) {
					map.put("flag", 1);//成功
				}else {
					map.put("flag", 2);//绑定失败
				}
			}else {//
				map.put("flag", 3);//手机号已绑定
			}
		}else {
			map.put("flag", 4);//手机号不存在
		}
		return map;
	}

	public int insertWchatTvUserInfo(WchatTvUserVo wchatTvUserVo, WchatTvOrgVo wchatTvOrgVo) {
		wchatTvUserVo.setF_wtut_status(2);
		int flag = wchatTvOrgDao.insertWchatTvUserInfo(wchatTvUserVo);
		if(flag > 0) {
			wchatTvOrgVo.setF_wtgo_weixinid(wchatTvUserVo.getF_wtut_id());
			wchatTvOrgVo.setF_wtgo_integral("0");
			wchatTvOrgVo.setF_wtgo_periods(1);
			flag = wchatTvOrgDao.insertWchatTvGameOrgInfo(wchatTvOrgVo);
			wchatTvOrgVo.setF_wtgo_periods(2);
			flag = wchatTvOrgDao.insertWchatTvGameOrgInfo(wchatTvOrgVo);
			wchatTvOrgVo.setF_wtgo_periods(3);
			flag = wchatTvOrgDao.insertWchatTvGameOrgInfo(wchatTvOrgVo);
		}
		return flag;
	}

	public WchatTvUserVo selectWchatTvUserInfo(String f_wtut_openid,String f_wtut_serverid) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("f_wtut_openid", f_wtut_openid);
		map.put("f_wtut_serverid", f_wtut_serverid);
		WchatTvUserVo wchatTvUserVo =  wchatTvOrgDao.selectWchatTvUserInfo(map);
		if(wchatTvUserVo == null) {
			wchatTvUserVo = wchatTvOrgDao.selectWchatTvUserInfoByServiceId(map);
		}
		return wchatTvUserVo;
	}

	public WchatTvUserVo getPhone(String userid) {
		WchatTvUserVo wcTvUserVo = wchatTvOrgDao.selectPhone(userid);
		return wcTvUserVo;
	}
	public int  selectByPhone(String f_wtut_phone) {
		return wchatTvOrgDao.selectByPhone(f_wtut_phone);
	}
	public int  selectByIPTV(String f_wtut_serverid) {
		return wchatTvOrgDao.selectByIPTV(f_wtut_serverid);
	}

	public Map<String,String> selectPhoneINFO(Map<String, Object> map2) {
		Map<String, String> map = new HashMap<String, String>();
		WchatTvUserVo  wchatTvOrgVo= wchatTvOrgDao.selectINFO(map2);
		if(wchatTvOrgVo == null) {
			map.put("msg", "未报名本次活动,请报名");
			map.put("result", "-1");
			return map;
		}
		String itvId = String.valueOf(wchatTvOrgVo.getF_wtut_serverid());
		if("null".equals(itvId) || "".equals(itvId)) {
			map.put("msg", "打开浙江IPTV电视登录斗地主大赛专区绑定iTV账号!");
			map.put("result", "-1");
			return map;
		}
		String phone = String.valueOf(wchatTvOrgVo.getF_wtut_phone());
		String BF;
		try {
			BF = BianfengService.getQueryBind(phone);
			JSONObject resultJson = JSONObject.parseObject(BF);
			String code = String.valueOf(resultJson.get("code"));
			if(!"0".equals(code)) {
				//边锋未绑定
				map.put("msg", "系统异常,请稍后再试!");
				map.put("result", "-1");
				return map;
			}else {
				String status = String.valueOf(JSONObject.parseObject(String.valueOf(resultJson.get("data"))).get("status"));
				if(!"0".equals(status)) {
					map.put("msg", "登录边锋游戏并绑定报名注册手机号!");
					map.put("result", "-1");
					return map;
				}
			}
		} catch (IOException e) {
			//LOG.error("话费兑换异常，用户："+phone);
			map.put("msg", "系统异常,请稍后再试!");
			map.put("result", "-1");
			return map;
		}
		//是否游戏10局
		try {
			String gameNum = BianfengService.getUserPlayCount(phone);
			JSONObject resultGame = JSONObject.parseObject(gameNum);
			String code1 = String.valueOf(resultGame.get("code"));
			if("0".equals(code1)) {
				JSONObject data = JSONObject.parseObject(String.valueOf(resultGame.get("data")));
				JSONObject phoneData = JSONObject.parseObject(String.valueOf(data.get(phone)));
				int count = Integer.parseInt(String.valueOf(phoneData.get("count")));
				if(count < 10) {
					map.put("msg", "在比赛专用房间内累积打满10局!");
					map.put("result", "-1");
					return map;
				}
			}else {
				//LOG.debug("兑换话费边锋查询游戏局数异常，用户："+phone);
				map.put("msg", "系统异常,请稍后再试!");
				map.put("result", "-1");
				return map;
			}
		} catch (IOException e) {
			map.put("msg", "系统异常,请稍后再试!");
			map.put("result", "-1");
			return map;
		}
		//满足情况
		AcTelephoneVo acTel =  wchatTvOrgDao.selectPhoneINFO(wchatTvOrgVo.getF_wtut_serverid());
		if(acTel == null) {
			//插入一条
			AcTelephoneVo selectPhone =  wchatTvOrgDao.selectPhoneINFO("");
			AcTelephoneVo insertVo = new AcTelephoneVo();
			insertVo.setF_apb_id(selectPhone.getF_apb_id());
			insertVo.setF_apb_user(wchatTvOrgVo.getF_wtut_serverid());
			int insertNum = wchatTvOrgDao.updateAcTelPhone(insertVo);
			System.out.println("状态修改成功--"+insertNum);
			map.put("result", "0");
			map.put("msg", "恭喜您获得10元电信话费");
			map.put("cardNo", selectPhone.getF_apb_cardnumber());
			map.put("cardKey", selectPhone.getF_apb_cardkey());
		}else {
			map.put("result", "0");
			map.put("msg", "恭喜您获得10元电信话费");
			map.put("cardNo", acTel.getF_apb_cardnumber());
			map.put("cardKey", acTel.getF_apb_cardkey());
		}
		return map;
	}

	public int selectInsertBF(AcInterfaceVo acVo) {
		int result = 1;
		Map<String,Object> map = new HashMap<>();
		map.put("f_wtut_phone", acVo.getUserPhone());
		WchatTvUserVo  wchatTvOrgVo= wchatTvOrgDao.selectINFO(map);
		if(wchatTvOrgVo == null) {
			WchatTvUserVo  wtoVo= new WchatTvUserVo();
			wtoVo.setF_wtut_openid("bianfeng");
			String itvId = String.valueOf(acVo.getIptvid());
			if("null".equals(itvId) || "".equals(itvId)) {
				itvId = "";
			}
			wtoVo.setF_wtut_serverid(itvId);
			wtoVo.setF_wtut_phone(acVo.getUserPhone());
			wtoVo.setF_wtut_nickname(acVo.getUserName());
			wtoVo.setF_wtut_status(2);
			wtoVo.setF_wtut_memo("");
			int flag = wchatTvOrgDao.insertWchatTvUserInfo(wtoVo);
			if(flag > 0) {
				WchatTvUserVo wtv = wchatTvOrgDao.selectINFO(map);
				SysAreaStreetVo sasv = wchatTvOrgDao.selectAreaID(acVo.getAreaName());
				//默认社区 随机前700社区
				SecureRandom random = new SecureRandom();
				int areaName = random.nextInt(700)+1;
				//int areaName = 249;
				if(sasv == null) {
					//未匹配到社区----------
					//System.out.println("未匹配到数据库社区："+acVo.getAreaName());
				}else {
					areaName = sasv.getF_sas_id();
				}
				WchatTvOrgVo wtov = new WchatTvOrgVo();
				wtov.setF_wtgo_weixinid(wtv.getF_wtut_id());
				wtov.setF_wtgo_integral("0");
				wtov.setF_wtgo_streeid(areaName);
				wtov.setF_wtgo_periods(1);
				flag = wchatTvOrgDao.insertWchatTvGameOrgInfo(wtov);
				wtov.setF_wtgo_periods(2);
				flag = wchatTvOrgDao.insertWchatTvGameOrgInfo(wtov);
				wtov.setF_wtgo_periods(3);
				flag = wchatTvOrgDao.insertWchatTvGameOrgInfo(wtov);
			}else {
				result = -1;
			}
		}
		return result;
	}
}
